#ifndef LENS_CONTROL_H
#define LENS_CONTROL_H

#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>

class LensControl
{
public:
	LensControl();
	~LensControl();

	bool open(const QString& portname);
	void close();
	bool isOpen();

	QString version();
	bool systemInit(uchar& lensID, ushort& curFocus, QVector<ushort>& focusRange, ushort& maxFocus, ushort& curAperture, QVector<ushort>& apertureRange);
	bool setFocusPos(ushort value);
	bool setAperturePos(ushort value);
	int getFocusPos();
	int getAperture(QVector<ushort>& apertureRange);
	int getFocus();

private:
	uchar verifyCode(const QVector<uchar>& codes);
	void sendCode(const QVector<uchar>& codes);
	bool recvCode(QVector<uchar>& codes, int msec = 1);
	QByteArray convert(QByteArray& bytes);
	
	QSerialPort* serialPort;
	QVector<ushort> focusRange;
	QVector<ushort> apertureRange;
	ushort maxFocus;

	uchar lensID;
	ushort currentFocus;
	ushort currentFocusPos;
	ushort currentAperture;

	QByteArray recv_bytes;
	bool isFinished = false;

	const uchar version_code = 0x00;
	const uchar init_code = 0x30;
	const uchar set_focus_code = 0x31;
	const uchar set_aperture_code = 0x32;
	const uchar get_focus_pos_code = 0x33;
	const uchar get_aperture_code = 0x34;
	const uchar get_focus_range_code = 0x35;
};

#endif
